package com.lynch.cxsqw.view;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.Gallery;

import com.lynch.adment.AsyncImageLoader;
import com.lynch.adment.GalleryCallbackImpl;
import com.lynch.adment.ImageAdapter;
import com.lynch.cxsqw.BaseActivity;
import com.lynch.cxsqw.R;

public class MainActivity extends BaseActivity {

	private AsyncImageLoader loader;
	private List<Bitmap> bitmapList;
	private List<String> imageList;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		buildAsyncImageLoader();
		buildGallery();
	}

	/**
	 * 创建 Gallery
	 */
	private void buildGallery() {

		Gallery gallery = (Gallery) findViewById(R.id.adment);
		imageList = buildImageList();
		bitmapList = new ArrayList<Bitmap>();
		Iterator<String> iterator = imageList.iterator();
		while (iterator.hasNext()) {
			String url = iterator.next();
			loadImage(url, gallery, bitmapList, imageList.size());
		}
	}

	/**
	 * 初始化 AsyncImageLoader
	 */
	private void buildAsyncImageLoader() {

		loader = new AsyncImageLoader(getScreenWidth());
	}

	/**
	 * 下载图片
	 */
	private void loadImage(final String url, Gallery gallery,
			List<Bitmap> bitmapList, int bitmapCount) {

		GalleryCallbackImpl galleryCallbackImpl = new GalleryCallbackImpl(
				gallery, bitmapList, bitmapCount, this);

		Bitmap cacheBitmap = loader.loadBitmap(url, galleryCallbackImpl);
		if (cacheBitmap != null) {
			bitmapList.add(cacheBitmap);
			if (bitmapList.size() == imageList.size()) {
				ImageAdapter imageAdapter = new ImageAdapter(this, bitmapList);
				gallery.setAdapter(imageAdapter);
			}
		}

	}

	/**
	 * 模拟构造服务器返回的广告数据
	 */
	private List<String> buildImageList() {

		List<String> imageList = new ArrayList<String>();
		// imageList.add("http://img13.360buyimg.com/da/20111025/766_120_qCjOUU.jpg");
		// imageList.add("http://img11.360buyimg.com/da/20111025/766_120_BBWxOg.jpg");
		// imageList.add("http://img14.360buyimg.com/da/20111024/766_120_NolXCc.jpg");
		// imageList.add("http://img13.360buyimg.com/da/20111024/766_120_pAXcNZ.jpg");
		// imageList.add("http://img11.360buyimg.com/da/20111024/766_120_OoyicD.jpg");

		// imageList.add("http://www.cxsqw.com/upload/13051875228546.jpg");
		// imageList.add("http://www.cxsqw.com/upload/13055985377912.jpg");
		// imageList.add("http://www.cxsqw.com/upload/13082896262890.jpg");
		// imageList.add("http://www.cxsqw.com/upload/13051875985257.jpg");
		// imageList.add("http://www.cxsqw.com/upload/13189084648900.jpg");

		imageList.add("http://img04.taobaocdn.com/tps/i4/T1cx1vXo0CXXXXXXXX-490-170.png");
		imageList.add("http://i.mmcdn.cn/simba/img/T1SHawXjXWXXXXXXXX.jpg?noq=y");
		imageList.add("http://i.mmcdn.cn/simba/img/T1UXuwXdBcXXXXXXXX.jpg?noq=y");
		imageList.add("http://i.mmcdn.cn/simba/img/T1kQyvXg0TXXXXXXXX.jpg?noq=y");
		imageList.add("http://img02.taobaocdn.com/tps/i2/T1ZcOwXeJAXXXXXXXX-490-170.png");

		return imageList;
	}


}